from django.db import models
from django.contrib.auth.models import User


class HostGroup(models.Model):
    """主机组"""
    name = models.CharField(max_length=100, unique=True, verbose_name='组名')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'host_group'
        verbose_name = '主机组'
        verbose_name_plural = '主机组'

    def __str__(self):
        return self.name


class Project(models.Model):
    """项目"""
    name = models.CharField(max_length=100, unique=True, verbose_name='项目名')
    code = models.CharField(max_length=50, unique=True, verbose_name='项目代码')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'project'
        verbose_name = '项目'
        verbose_name_plural = '项目'

    def __str__(self):
        return self.name


class Environment(models.Model):
    """环境"""
    ENV_CHOICES = [
        ('dev', '开发环境'),
        ('test', '测试环境'),
        ('staging', '预发布环境'),
        ('prod', '生产环境'),
    ]
    name = models.CharField(max_length=100, unique=True, verbose_name='环境名')
    env_type = models.CharField(max_length=20, choices=ENV_CHOICES, verbose_name='环境类型')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'environment'
        verbose_name = '环境'
        verbose_name_plural = '环境'

    def __str__(self):
        return self.name


class Host(models.Model):
    """主机"""
    hostname = models.CharField(max_length=100, unique=True, verbose_name='主机名')
    ip_address = models.GenericIPAddressField(verbose_name='IP地址')
    port = models.IntegerField(default=22, verbose_name='SSH端口')
    username = models.CharField(max_length=50, verbose_name='SSH用户名')
    password = models.CharField(max_length=255, blank=True, verbose_name='SSH密码')
    private_key = models.TextField(blank=True, verbose_name='SSH私钥')
    
    host_group = models.ForeignKey(
        HostGroup, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True,
        related_name='hosts',
        verbose_name='主机组'
    )
    project = models.ForeignKey(
        Project,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='hosts',
        verbose_name='所属项目'
    )
    environment = models.ForeignKey(
        Environment,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='hosts',
        verbose_name='所属环境'
    )
    
    os_type = models.CharField(max_length=50, blank=True, verbose_name='操作系统')
    cpu_cores = models.IntegerField(null=True, blank=True, verbose_name='CPU核心数')
    memory_size = models.IntegerField(null=True, blank=True, verbose_name='内存大小(MB)')
    disk_size = models.IntegerField(null=True, blank=True, verbose_name='磁盘大小(GB)')
    
    status = models.CharField(
        max_length=20,
        choices=[
            ('online', '在线'),
            ('offline', '离线'),
            ('maintenance', '维护中'),
        ],
        default='offline',
        verbose_name='状态'
    )
    
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'host'
        verbose_name = '主机'
        verbose_name_plural = '主机'

    def __str__(self):
        return f"{self.hostname} ({self.ip_address})"


class UserGroup(models.Model):
    """用户组"""
    name = models.CharField(max_length=100, unique=True, verbose_name='组名')
    description = models.TextField(blank=True, verbose_name='描述')
    users = models.ManyToManyField(User, related_name='user_groups', blank=True, verbose_name='用户')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'user_group'
        verbose_name = '用户组'
        verbose_name_plural = '用户组'

    def __str__(self):
        return self.name
